package com.xuyiwei.mayi.test;

/**
 * Created by xuyiwei on 2019/4/13.
 */
class VolatileThreadRunnable extends Thread{

    public volatile boolean flag = true;

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

//    public  volatile boolean flag = false;
//    @Override
//    public void run() {
//        System.out.println("开始执行子线程....");
//        while (flag) {
//        }
//        System.out.println("线程停止");
//    }
    public void setRuning(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        while (flag){

        }
        System.out.println("flag已经设置成true了");

    }
}

public class VolatileThread {
    public static void main(String[] args) throws InterruptedException {
        VolatileThreadRunnable t1 = new VolatileThreadRunnable();
        t1.start();
        Thread.sleep(3000);
        t1.setRuning(false);
//        Thread.sleep(1000);
        System.out.println(t1.flag);
    }
}
